from django.utils.decorators import method_decorator

from gis.common.django_ext.decorators import validate_parameters
from gis.common.django_ext.views import BaseView, PaginationResponse
from gis.common.exceptions import BizException
from itpay.core.agency import receipt_service
from itpay.core.agency.const import AgencyType
from itpay.core.agency.errors import ERROR_RECEIPT_ADDRESS_NOT_EXISTS
from itpay.server_agency.decorators import require_login
from itpay.server_agency.view.agency import receipt_form
from .helper import wrapper_receipt_address


class ReceiptConfView(BaseView):
    def get(self, request):
        return dict(conf=receipt_service.get_all_receipt_conf())


class ReceiptAddressListView(BaseView):
    @method_decorator(require_login())
    @method_decorator(validate_parameters(receipt_form.QueryReceiptAddressSchema))
    def get(self, request, cleaned_data):
        user = request.user
        if AgencyType(user['agency_type']) == AgencyType.ADMIN_AGENCY:
            cleaned_data['parent_id'] = user['id']
        elif AgencyType(user['agency_type']) == AgencyType.NORMAL_AGENCY:
            cleaned_data['agency_id'] = user['id']
        c, ls = receipt_service.list_receipt_addresses(**cleaned_data)
        return PaginationResponse(c, [wrapper_receipt_address(e, wrap_agency=True, wrap_reached_daily_limit=True,
                                                              wrap_available=True) for e in ls])

    @method_decorator(require_login(check_vcode=True))
    @method_decorator(validate_parameters(receipt_form.AddReceiptAddressSchema))
    def post(self, request, cleaned_data):
        agency_id = request.user_id
        return receipt_service.create_receipt_address(agency_id, **cleaned_data)


class ReceiptAddressDetailView(BaseView):
    @method_decorator(require_login())
    def get(self, request, receipt_id):
        agency_id = request.user_id
        receipt = receipt_service.get_receipt_address(receipt_id)
        if receipt['agency_id'] != agency_id:
            raise BizException(ERROR_RECEIPT_ADDRESS_NOT_EXISTS)
        return receipt_service.get_receipt_address(receipt_id)

    @method_decorator(require_login())
    def delete(self, request, receipt_id):
        agency_id = request.user_id
        receipt = receipt_service.get_receipt_address(receipt_id)
        if receipt['agency_id'] != agency_id:
            raise BizException(ERROR_RECEIPT_ADDRESS_NOT_EXISTS)
        receipt_service.delete_receipt_address(receipt_id)

    @method_decorator(require_login(check_vcode=True))
    @method_decorator(validate_parameters(receipt_form.UpdateReceiptAddressSchema))
    def put(self, request, receipt_id, cleaned_data):
        agency_id = request.user_id
        receipt = receipt_service.get_receipt_address(receipt_id)
        if receipt['agency_id'] != agency_id:
            raise BizException(ERROR_RECEIPT_ADDRESS_NOT_EXISTS)
        return receipt_service.update_receipt_address(receipt_id, **cleaned_data)


class ReceiptAddressStatusView(BaseView):
    @method_decorator(require_login(check_vcode=True))
    @method_decorator(validate_parameters(receipt_form.UpdateReceiptAddressStatusSchema))
    def put(self, request, receipt_id, cleaned_data):
        receipt_service.update_receipt_address_status(receipt_id, **cleaned_data)
